<?

include "../map-config.php";

@mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
@mysql_select_db(DB_NAME);

if($_GET['lat']) $lat = (int)$_GET['lat'];
if($_GET['lng']) $lng = (int)$_GET['lng'];
if($_GET['rad']) $distance = (int)$_GET['rad'];

/*
$lat = 45.43;
$lng = -75.72;
*/
$latitude = "latitude";
$longitude = "longitude";

$sql = "SELECT email, ( 3959 * acos( cos( radians($lat) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( $latitude ) ) ) ) AS distance FROM geo_addresses HAVING distance < $distance ORDER BY distance";

$query = mysql_query($sql) or die(mysql_error());
$info = mysql_fetch_array($query);

$count = 0;
while($info) {
	echo "";
	echo htmlentities($info[email]);
	echo "\n";
	$info = mysql_fetch_array($query);
	$count++;
}
/*
if($count < 1) {
	echo "<p><strong>No addresses found in this area.</strong></p>";
}
else {
	echo "<p><strong>$count addresses found in this area.</strong></p>";
}*/
?>